package com.livery.app.model.tables;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import org.hibernate.Session;

import com.livery.app.main.HibernateUtil;
import com.livery.data.Transaction;

public class TransactionTableModel extends AbstractTableModel{
	
	private List<Transaction> transationList;
	private ArrayList<TableModelListener> listeners = new ArrayList<TableModelListener>();
	private final String[] columnHeaders= {"Klient","Samochod","Pracownik wydający", "Pracownik przyjmujący", "Data wydania" , "Termin zwrotu", "Data Zwrotu","Cena"};
	
	public TransactionTableModel(List<Transaction> transactionList){
		this.transationList = transactionList;
	}
	
	public TransactionTableModel(){
		this.transationList = new ArrayList<Transaction>() ;
	}
	
	
	
	public List<Transaction> getTransactionList() {
		return transationList;
	}

	public void setTransactionList(List<Transaction> transationList) {
		this.transationList = transationList;
	}

	@Override
	public int getColumnCount() {
		return 8;
	}

	@Override
	public int getRowCount() {
		return transationList.size();
	}

	@Override
	public Object getValueAt(int r, int c) {
		
		switch(c)
		{
		case 0 : return transationList.get(r).getClient().getLastName()+" "+transationList.get(r).getClient().getFirstName() + "\n"+transationList.get(r).getClient().getDocumentID();
		case 1 : return transationList.get(r).getCar().getModel() + " "+transationList.get(r).getCar().getMake();
		case 2 : return transationList.get(r).getPickWorker().getLastName().toString();
		case 3 : return (transationList.get(r).getDropWorker()==null) ? "" : transationList.get(r).getDropWorker().getLastName().toString();
		case 4 : return transationList.get(r).getPickDate().toString();
		case 5 : return transationList.get(r).getDeadlineDate().toString();
		case 6 : return (transationList.get(r).getDropDate() == null) ? "" :transationList.get(r).getDropDate().toString();
		case 7 : return transationList.get(r).getPrice().toString();
		default : return null;
		}
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		
		return String.class;
		
	}
	
	public void addRow(Transaction transaction){
		transationList.add(transaction);
		TableModelEvent event = new TableModelEvent(this,transationList.size()-1,transationList.size()-1,TableModelEvent.ALL_COLUMNS,TableModelEvent.INSERT);
		for(TableModelListener tml : listeners){
			tml.tableChanged(event);
		}
	}
	
	public void updateRow(int index, Transaction transaction){
		
		transationList.set(index, transaction);
		TableModelEvent event = new TableModelEvent( this, index, index, TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE );
		for(TableModelListener tml : listeners){
			tml.tableChanged(event);
		}
	}
	
	public void removeRow(Transaction transaction){
		int index = transationList.indexOf(transaction);
		transationList.remove(index);
		
		TableModelEvent event = new TableModelEvent( this, index, index, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE );
		for(TableModelListener tml : listeners){
			tml.tableChanged(event);
		}
		
	}
	
	@Override
	public String getColumnName(int column) {
		
		return columnHeaders[column];
	}
	
	@Override
	public void addTableModelListener(TableModelListener l) {
		if(listeners.contains(l)){
			return; 
		}
		
		listeners.add(l);
	}
	
	@Override
	public void removeTableModelListener(TableModelListener l) {
		listeners.remove(l);
	}

}
